package com.yuwei.recyclerviewdemo;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.yuwei.recyclerviewdemo.adapter.CommonAdapter;
import com.yuwei.recyclerviewdemo.adapter.HasLoadMoreAdapter;
import com.yuwei.recyclerviewdemo.adapter.MultiItemCommonAdapter;
import com.yuwei.recyclerviewdemo.adapter.MultiItemTypeSupport;
import com.yuwei.recyclerviewdemo.adapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements MySwipeRefreshLayout.MyScollListener {
    public static final String TAG = "MainActivity";
    @BindView(R.id.activity_main_rlv)
    RecyclerView recyclerView;
    @BindView(R.id.activity_main_myslv)
    MySwipeRefreshLayout mySwipeRefreshLayout;
    private List<String> mList;
    private GridLayoutManager mLayoutManager;
    private HasLoadMoreAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mList = new ArrayList<>();
        initData();
        initWidget();
    }

    private void initWidget() {
        // 设置RecyclerView布局
        mLayoutManager = new GridLayoutManager(MainActivity.this, 1);
        recyclerView.setLayoutManager(mLayoutManager);
//        recyclerView.setAdapter(myAdapter());
        mySwipeRefreshLayout.setAdapter(hasLoadMoreAdapter());
        if (mList.size() < 10) {
            myAdapter.setLoadMore(true);
        }
        mySwipeRefreshLayout.setRefreshColors(Color.BLUE, Color.GREEN, Color.RED, Color.YELLOW);
        mySwipeRefreshLayout.setRefreshEnabled(true);
        mySwipeRefreshLayout.setScrollBareFadeDuration(4000);
        mySwipeRefreshLayout.setMyScollListener(this);
        mySwipeRefreshLayout.setInRefreshListener(myRefreshListener());
    }

    private SwipeRefreshLayout.OnRefreshListener myRefreshListener() {
        SwipeRefreshLayout.OnRefreshListener myListener = new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mySwipeRefreshLayout.setIsRefreshing(true);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mList.clear();
                        initData();
                        myAdapter.notifyDataSetChanged();
                        if (mySwipeRefreshLayout.getIsRefreshing()) {
                            mySwipeRefreshLayout.setIsRefreshing(false);
                        }
                    }
                }, 1000);
            }
        };
        return myListener;
    }

    private void initData() {
        for (int i = 0; i < 20; i++) {
            String str = "第" + (i + 1) + "个item";
            mList.add(str);
        }
    }

    private RecyclerView.Adapter hasLoadMoreAdapter() {
        myAdapter = new HasLoadMoreAdapter<String>(getApplicationContext(), mList, multiItemTypeSupport) {
            @Override
            public void convert(ViewHolder holder, String s) {
                if (getItemType(holder.getMPosition()) == 0) {
                    holder.setText(R.id.item_adpater_first_tv, s);
                }
            }
        };
        return myAdapter;
    }

    private RecyclerView.Adapter myAdapter() {
        RecyclerView.Adapter myAdapter = new CommonAdapter<String>(getApplicationContext(), R.layout.item_adapter_first, mList) {
            @Override
            public void convert(ViewHolder holder, String s) {
                holder.setText(R.id.item_adpater_first_tv, s);
            }
        };
        return myAdapter;
    }

    private RecyclerView.Adapter myHeadAdapter() {
        RecyclerView.Adapter myAdapter = new MultiItemCommonAdapter<String>(getApplicationContext(), mList, multiItemTypeSupport) {
            @Override
            public void convert(ViewHolder holder, String s) {
                holder.setText(R.id.item_adpater_first_tv, s);
            }
        };
        return myAdapter;
    }

    private MultiItemTypeSupport multiItemTypeSupport = new MultiItemTypeSupport() {
        @Override
        public int getLayoutId(int itemType) {
            switch (itemType) {
                case 1:
                    return R.layout.loadmore_layout;
                case 0:
                default:
                    return R.layout.item_adapter_first;
            }
        }

        @Override
        public int getItemViewType(int position, Object o) {
            if (myAdapter.getItemCount() - position == 1 && myAdapter.getItemCount() == mList.size() + 1) {
                return 1;
            } else {
                return 0;
            }
        }
    };

    @Override
    public void myScroll() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                initData();
                myAdapter.notifyDataSetChanged();
            }
        }, 1000);
    }
}
